/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SummaryPanel.java
 *
 * Created on Nov 7, 2010, 5:17:56 PM
 */

package lmclogistics;

import components.PeriodToCover;
import components.Lightbox;
import components.Button;
import components.NButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import sql.Sql;

/**
 *
 * @author Sonia Tan
 */
public class SummaryPanel extends javax.swing.JPanel {

    /** Creates new form SummaryPanel */

    private int j = 0;
    private Button summarySubmenu[] = new Button[5];
    private JPanel pl;
    private PeriodToCover ptc;
    private JComboBox customerField = new JComboBox();

    public SummaryPanel() {
        initComponents();
        initSumMenus();
        setOpaque(false);
        setLocation(613, 165);
        setSize(287, 375);
        initButtons();
        setLayout(null);
    }

    public void setSumButton(String button,String rollover,int x, int y){
        summarySubmenu[j] = new Button(button,291,37);
        summarySubmenu[j].setLocation(x,y);
        summarySubmenu[j].setRollover(rollover);
        add(summarySubmenu[j]);
        j++;


    }

    public void initSumMenus(){
        setSumButton("eirsummary button.png","eirsummary button selected.png",0,0);
        setSumButton("inreport button.png","inreport button selected.png",0,40);
        setSumButton("outreport button.png","outreport button selected.png",0,80);
        setSumButton("sur button.png","sur button selected.png",0,120);
        setSumButton("surc button.png","surc button selected.png",0,160);


    }

     public void initButtons(){

        summarySubmenu[0].addActionListener(new ActionListener(){
            JButton ok;
            JButton back;
            public void actionPerformed(ActionEvent e) {
                summarySubmenu[0].setEnabled(false);
                for(int i=1;i<j;i++)
                     summarySubmenu[i].setVisible(false);

                ptc = new PeriodToCover();

                pl = ptc;
                pl.setLocation(0,20);
                add(pl,1);
               
                repaint();
                ok = new NButton("Ok",200,175);
                back = new NButton("Back",15,175);
                add(ok);
                add(back);

                back.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        remove(pl);
                        remove(ok);
                        remove(back);
                        setDefault();
                        repaint();
                    }
                });

                ok.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                       if(ptc.checkDate()==true)
                       {
                           Login.setDescription("");
                           remove(pl);
                           remove(ok);
                           remove(back);

                           String sDate, eDate;
                           sDate = ptc.getStartDate();
                           eDate = ptc.getEndDate();

                           for(int i=0;i<j;i++)
                                summarySubmenu[i].setVisible(false);

                           setSize(919,577);
                           setLocation(0,0);

                           EirSummary eirSummary= new EirSummary(sDate, eDate);

                           Main.login.disableButtons();

                            pl = new Lightbox(eirSummary);
                            add(pl, 1);

                            NButton close = new NButton("Close",740,480);
                            pl.add(close,0);
                            close.addActionListener(new ActionListener(){
                                public void actionPerformed(ActionEvent e) {
                                remove(pl);
                                setDefault();
                                repaint();
                                Main.login.enableButtons();
                                }

                            });
                       }
                       else
                           Login.setDescription("Please input valid dates.");
            }
        });
        }
        });

        summarySubmenu[1].addActionListener(new ActionListener(){
            NButton ok;
            NButton back;
            public void actionPerformed(ActionEvent e) {
                summarySubmenu[0].setEnabled(false);
                summarySubmenu[1].setEnabled(false);
                for(int i=2;i<j;i++)
                     summarySubmenu[i].setVisible(false);
                ptc = new PeriodToCover();

                pl = ptc;
                pl.setLocation(0,60);
                add(pl,1);

                repaint();
                ok = new NButton("Ok",200,215);
                back = new NButton("Back",15,215);
                add(ok);
                add(back);

                back.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        remove(pl);
                        remove(ok);
                        remove(back);
                        setDefault();
                        repaint();
                    }
                });

                ok.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                       if(ptc.checkDate()==true)
                       {
                           Login.setDescription("");
                               remove(pl);
                               remove(ok);
                               remove(back);

                               String sDate, eDate;
                               sDate = ptc.getStartDate();
                               eDate = ptc.getEndDate();

                               for(int i=0;i<j;i++)
                                    summarySubmenu[i].setVisible(false);

                               setSize(919,577);
                               setLocation(0,0);

                               InReport inReport = new InReport(sDate, eDate);

                               Main.login.disableButtons();

                                pl = new Lightbox(inReport);
                                add(pl, 1);

                                NButton close = new NButton("Close",740,480);
                                pl.add(close,0);
                                close.addActionListener(new ActionListener(){
                                    public void actionPerformed(ActionEvent e) {
                                    remove(pl);
                                    setDefault();
                                    repaint();
                                    Main.login.enableButtons();
                                    }

                                });
                       }
                        else
                           Login.setDescription("Please input valid dates.");
            }
        });
        }
        });

        summarySubmenu[2].addActionListener(new ActionListener(){
            NButton ok;
            NButton back;
            public void actionPerformed(ActionEvent e) {
                for(int i=0;i<3;i++)
                    summarySubmenu[i].setEnabled(false);
                for(int i=3;i<j;i++)
                     summarySubmenu[i].setVisible(false);

                ptc = new PeriodToCover();
                pl = ptc;
                pl.setLocation(0,100);
                add(pl,1);

                repaint();
                ok = new NButton("Ok",200,250);
                back = new NButton("Back",15,250);
                add(ok);
                add(back);

                back.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        remove(pl);
                        remove(ok);
                        remove(back);
                        setDefault();
                        repaint();
                    }
                });

                ok.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                       if(ptc.checkDate()==true)
                       {
                           Login.setDescription("");
                           remove(pl);
                           remove(ok);
                           remove(back);

                           String sDate, eDate;
                           sDate = ptc.getStartDate();
                           eDate = ptc.getEndDate();

                           for(int i=0;i<j;i++)
                                summarySubmenu[i].setVisible(false);

                           setSize(919,577);
                           setLocation(0,0);

                           OutReport outReport = new OutReport(sDate, eDate);

                           Main.login.disableButtons();

                            pl = new Lightbox(outReport);
                            add(pl, 1);

                            NButton close = new NButton("Close",740,480);
                            pl.add(close,0);
                            close.addActionListener(new ActionListener(){
                                public void actionPerformed(ActionEvent e) {
                                remove(pl);
                                setDefault();
                                repaint();
                                Main.login.enableButtons();
                                }
                            });
                        }
                        else
                            Login.setDescription("Please input valid dates.");
            }
        });
        }
        });
        summarySubmenu[3].addActionListener(new ActionListener(){
            NButton ok;
            NButton back;
            public void actionPerformed(ActionEvent e) {
                for(int i=0;i<4;i++)
                    summarySubmenu[i].setEnabled(false);
                for(int i=4;i<j;i++)
                     summarySubmenu[i].setVisible(false);

                ptc = new PeriodToCover();
                pl = ptc;
                pl.setLocation(0,140);
                add(pl,1);

                repaint();
                ok = new NButton("Ok",200,285);
                back = new NButton("Back",15,285);
                add(ok);
                add(back);

                back.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        remove(pl);
                        remove(ok);
                        remove(back);
                        setDefault();
                        repaint();
                    }
                });

                ok.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                       if(ptc.checkDate())
                       {
                           Login.setDescription("");
                           remove(pl);
                           remove(ok);
                           remove(back);

                           String sDate, eDate;
                           sDate = ptc.getStartDate();
                           eDate = ptc.getEndDate();

                           for(int i=0;i<j;i++)
                                summarySubmenu[i].setVisible(false);

                           setSize(919,577);
                           setLocation(0,0);

                           Sur surReport = new Sur(sDate, eDate);

                           Main.login.disableButtons();

                            pl = new Lightbox(surReport);
                            add(pl, 1);

                            NButton close = new NButton("Close",740,480);
                            pl.add(close,0);
                            close.addActionListener(new ActionListener(){
                                public void actionPerformed(ActionEvent e) {
                                remove(pl);
                                setDefault();
                                repaint();
                                Main.login.enableButtons();
                                }
                            });
                        }
                        else
                            Login.setDescription("Please input valid dates");
            }
        });
        }
        });

        summarySubmenu[4].addActionListener(new ActionListener(){
            NButton ok;
            NButton back;
            public void actionPerformed(ActionEvent e) {
                for(int i=0;i<5;i++)
                    summarySubmenu[i].setEnabled(false);                

                initBox();
                ptc = new PeriodToCover();
                pl = ptc;
                pl.setLocation(0,180);
                add(pl,1);

                repaint();
                ok = new NButton("Ok",200,325);
                back = new NButton("Back",15,325);
                add(ok);
                add(back);

                back.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        remove(pl);
                        remove(ok);
                        remove(back);
                        remove(customerField);
                        setDefault();
                        repaint();
                    }
                });

                ok.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                      if(ptc.checkDate() == true && customerField.getSelectedIndex() != 0){
                       
                               remove(pl);
                               remove(ok);
                               remove(back);
                               remove(customerField);

                               Login.setDescription("");
                               for(int i=0;i<j;i++)
                                    summarySubmenu[i].setVisible(false);

                               String sDate, eDate;
                               sDate = ptc.getStartDate();
                               eDate = ptc.getEndDate();


                               setSize(919,577);
                               setLocation(0,0);

                               SurC surCReport = new SurC(sDate, eDate, getCust());

                               Main.login.disableButtons();

                                pl = new Lightbox(surCReport);
                                add(pl, 1);

                                NButton close = new NButton("Close",740,480);
                                pl.add(close,0);
                                close.addActionListener(new ActionListener(){
                                        public void actionPerformed(ActionEvent e) {
                                        remove(pl);
                                        setDefault();
                                        repaint();
                                        Main.login.enableButtons();
                                        }


                                    });


                        }
                        else if(ptc.checkDate() == false)
                            Login.setDescription("Please input valid dates!");
                        else if(customerField.getSelectedIndex() == 0)
                            Login.setDescription("Please select a customer!");
                 }

        });
        }
        });
    }

     public void setDefault(){
            setLocation(613, 165);
            setSize(287, 375);

            for(int i=0;i<j;i++)
            {
                summarySubmenu[i].setVisible(true);
                summarySubmenu[i].setEnabled(true);
            }
        }

     public void initBox(){

        customerField.setLocation(60, 220);
        customerField.setSize(200,25);
        add(customerField, 0);
        customerField.removeAllItems();

        String custQuery = "SELECT custName FROM customer WHERE deleted=0";


        try{
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(custQuery);

           customerField.insertItemAt("<Customer>", 0);
           customerField.setSelectedIndex(0);
           int i = 1;

            while(rs.next())
            {
                customerField.insertItemAt(rs.getString(1),i);
                i++;
            }


            Sql.closeSqlConnection(connection);
        } catch(Exception ex){
            Logger.getLogger(EiroutPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

     public String getCust(){

         String custName = customerField.getSelectedItem().toString();

         return custName;
     }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
